package net.cjw.initsb.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import net.cjw.initsb.common.ServiceResult;
import net.cjw.initsb.dao.PermissionMapper;
import net.cjw.initsb.entity.Permission;
import net.cjw.initsb.service.PermissionServiceI;
import net.cjw.initsb.utils.UUIDBuild;

@Service
@Transactional
public class PermissionServiceImpl implements PermissionServiceI {

	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private PermissionMapper permissionMapper;

	@Override
	public ServiceResult<String> create(Permission entity) {
		// 检查
		Permission find;
		Permission rs;
		find = new Permission();
		find.setNodeName(entity.getNodeName());

		rs = permissionMapper.selectOne(find);
		if (rs != null) {
			return ServiceResult.buildError("节点名称已被使用");
		}
		// 创建菜单权限和操作权限 验证父节点
		if (entity.getState() == 1 || entity.getState() == 2) {
			find = new Permission();
			find.setNodeName(entity.getPid());
			rs = permissionMapper.selectOne(find);
			if (rs == null) {
				return ServiceResult.buildError("父节点不存在");
			} else {
				entity.setPid(rs.getId());
			}
		}

		// 插入
		entity.setCreateTime(new Date());
		entity.setId(UUIDBuild.createUUId());
		permissionMapper.insert(entity);
		return ServiceResult.buildSuccess();
	}

	@Override
	public ServiceResult<String> update(Permission entity) {
		// 检查
		Permission find;
		Permission rs;
		Permission updateEntity;
		find = new Permission();
		find.setId(entity.getId());
		rs = permissionMapper.selectOne(find);
		if (rs == null) {
			return ServiceResult.buildError("节点不存在,无法更新");
		}
		updateEntity = rs;
		if (entity.getNodeName() != null && !updateEntity.getNodeName().equals(entity.getNodeName())) {
			find = new Permission();
			find.setNodeName(entity.getNodeName());
			rs = permissionMapper.selectOne(find);
			if (rs != null) {
				return ServiceResult.buildError("节点名称已被使用,无法更新");
			}
			updateEntity.setNodeName(entity.getNodeName());
		}
		updateEntity.setPermissionValue(entity.getPermissionValue());
		updateEntity.setUrl(entity.getUrl());
		// 修改
		// int i = permissionMapper.updateByPrimaryKeySelective(entity);
		int i = permissionMapper.updateByPrimaryKeySelective(updateEntity);
		if (i == 0) {
			return ServiceResult.buildError("修改失败");
		}
		return ServiceResult.buildSuccess();
	}

	@Override
	public ServiceResult<String> delete(Permission entity) {
		Permission find;
		Permission rs;
		Permission deleteEntity;
		find = new Permission();
		find.setId(entity.getId());
		rs = permissionMapper.selectOne(find);
		if (rs == null) {
			return ServiceResult.buildError("节点不存在,无法删除");
		}
		deleteEntity = rs;
		permissionMapper.delete(deleteEntity);
		String pid = null;
		List<Map<String, Object>> list = permissionMapper.selectByStateAndPId(null, rs.getId());
		for (Map<String, Object> map : list) {
			pid = (String) map.get("id");
			deleteEntity = new Permission();
			deleteEntity.setId(pid);
			permissionMapper.delete(deleteEntity);
			List<Map<String, Object>> list2 = permissionMapper.selectByStateAndPId(null, pid);
			for (Map<String, Object> map2 : list2) {
				pid = (String) map2.get("id");
				deleteEntity = new Permission();
				deleteEntity.setId(pid);
				permissionMapper.delete(deleteEntity);
			}
		}
		return ServiceResult.buildSuccess();
	}

	@Override
	public ServiceResult<Object> selectAll(Integer start, Integer max) {
		PageHelper page = new PageHelper();
		page.startPage(start, max);
		List<Permission> list = permissionMapper.selectAll();
		PageInfo<Permission> pageInfo = new PageInfo<Permission>(list);
		return ServiceResult.buildSuccess(pageInfo);
	}

	@Override
	public ServiceResult<Permission> selectOne(Permission entity) {
		return null;
	}

	@Override
	public ServiceResult<List<Map<String, Object>>> selectByTreeOrder() {

		List<Map<String, Object>> dirList = permissionMapper.selectByStateAndPId(0, null);
		logger.info(dirList.size() + "");
		List<Map<String, Object>> rs = new ArrayList<>();
		rs = buildTreeList(dirList, 0, rs);
		return ServiceResult.buildSuccess(rs, (long) rs.size());
	}

	private List<Map<String, Object>> buildTreeList(List<Map<String, Object>> list, int count,
			List<Map<String, Object>> rs) {

		count++;
		logger.info("count: " + count);
		if (count > 3) {
			// 节点错乱,立即返回
			return rs;
		}

		for (Map<String, Object> map : list) {
			String pid = (String) map.get("id");
			Integer state = (Integer) map.get("state");
			logger.info("node_name: " + map.get("node_name"));
			rs.add(map);
			List<Map<String, Object>> temp = permissionMapper.selectByStateAndPId(null, pid);
			if (temp.isEmpty()) {
				continue;
			} else {

				buildTreeList(temp, count, rs);
			}

		}
		logger.info("rs.size(): " + rs.size());
		return rs;

	}

	@Override
	public ServiceResult<List<Map<String, Object>>> selectMenuByListOrder() {
		// 查询所有根节点
		List<Map<String, Object>> dirList = permissionMapper.selectByStateAndPId(0, null);
		for (Map<String, Object> dirMap : dirList) {
			String dirid = (String) dirMap.get("id");
			List<Map<String, Object>> menuList = permissionMapper.selectByStateAndPId(null, dirid);
			if (!menuList.isEmpty()) {
				dirMap.put("list", menuList);
			}
		}

		return ServiceResult.buildSuccess(dirList);
	}

	@Override
	public ServiceResult<List<Map<String, Object>>> selectZtreeData() {
		return ServiceResult.buildSuccess(permissionMapper.selectZtreeData());
	}

}